﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Web.Model;
using Portal.Utility;
using Portal.Web.Utility;
using Portal.Exam.Bussiness;
using Portal.Exam.Model;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class AddYesNo
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut=new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 

                string[] Answer = new string[]{
                    "-- Chọn --","Đúng","Sai"
                };
                string[] AnswerCode = new string[]{
                    "","Yes","No"
                };

                string SelectAnswerText = "<select id=drpSelectAns>";
                for (int iIndex = 0; iIndex < Answer.Length; iIndex++)
                {
                    SelectAnswerText += "<option value=\"" + AnswerCode[iIndex]+ "\">" + Answer[iIndex] + "</option>";
                }
                SelectAnswerText += "</select>";

                string MonthiId = (string)WebEnvironments.Request(SiteId, "MonthiId");
                string Type = (string)WebEnvironments.Request(SiteId, "Type");
                string SelectLevel =
                     "<select id=drpSelectLevelQuestion>\r\n" +
                     "   <option value=\"0\">Mức độ dễ</option>\r\n" +
                     "   <option value=\"1\">Mức độ trung bình</option>\r\n" +
                     "   <option value=\"2\">Mức độ khó</option>\r\n" +
                     "</select>\r\n";
                //string BackUrl = "ExamQuestion.aspx";
                string RetHtml =
                    " <script language=javascript> \r\n" +
                    " function ClientSaveAddYesNo() \r\n" +
                    " { \r\n" +
                    "     SiteId='" + SiteId + "';\r\n" +
                    "     Subject=document.getElementById(\"txtQuestion\").value; \r\n" +
                    "     Index=document.getElementById(\"drpSelectAns\").selectedIndex; \r\n" +
                    "     MonthiId=document.getElementById(\"txtMonthiId\").value; \r\n" +
                    "     Point=document.getElementById(\"txtPoint\").value; \r\n" +
                    "     SecondTime = document.getElementById(\"txtSecondTime\").value; \r\n" +
                    "     IsMemory = document.getElementById(\"chkIsMemory\").checked; \r\n" +
                    "     SelectLevelIndex = document.getElementById(\"drpSelectLevelQuestion\").selectedIndex; \r\n" +
                    "     SelectLevel = document.getElementById(\"drpSelectLevelQuestion\").options[SelectLevelIndex].value; \r\n" +
                    "     SecondHidden = document.getElementById(\"txtSecondHidden\").value; \r\n" +
                    "     QuestionId = Portal.Exam.Render.AddYesNo.ServerSideAddYesNo(SiteId, MonthiId, Subject, Index, Point, SecondTime, IsMemory, SecondHidden, SelectLevel).value; \r\n" +
                    "     if(QuestionId.indexOf(\"ERROR\")!=-1) \r\n" +
                    "     { \r\n" +
                    "         alert(QuestionId); \r\n" +
                    "         return; \r\n" +
                    "     } \r\n" +
                    "     document.getElementById(\"txtQuestion\").value=\"\"; \r\n" +
                    "     document.getElementById(\"txtPoint\").value=\"1\"; \r\n" +
                    "     document.getElementById(\"txtSecondTime\").value=\"60\"; \r\n" +
                    "     document.getElementById(\"ctl04\").focus(); \r\n" +
                    "     ShowClientUpdateUrlForm(QuestionId); \r\n" +
                    " } \r\n" +
                    
                    " function ShowClientUpdateUrlForm(QuestionId) \r\n" +
                    " { \r\n" +
                    "     SiteId='" + SiteId + "';\r\n" +
                    "     Url=Portal.Exam.Render.AddYesNo.GetShowUpdateForm(SiteId, QuestionId).value; \r\n" +
                    "     if(Url.indexOf(\"ERROR\")!=-1) \r\n" +
                    "     { \r\n" +
                    "         alert(Url); \r\n" +
                    "         return; \r\n" +
                    "     } \r\n" +
                    "     window.open(Url, '_self'); \r\n" +
                    " } \r\n" +
                    " </script>\r\n" +
                    "   <input type=hidden id=txtMonthiId value=\"" + MonthiId + "\">" +
                    "   <input type=hidden id=txtType value=\"" + Type + "\">" +
                    "   <div class=FormSaveUpdate>\r\n" +
                    "      <table>" +
                    "         <tr>" +
                    "              <td class=\"Col1\">Mức độ</td>" +
                    "              <td class=\"Col2\">" + SelectLevel + "</td>" +
                    "         </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Nội dung câu hỏi:</td>" +
                    "              <td class=\"Col2\"><textarea class=cssTextBoxQuestion id=txtQuestion></textarea></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Điểm:</td>" +
                    "              <td class=\"Col2\"><input id=txtPoint value=\"1\"></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Thời gian(giây):</td>" +
                    "              <td class=\"Col2\"><input id=txtSecondTime value=\"30\"></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Đáp án:</td>" +
                    "              <td class=\"Col2\">" + SelectAnswerText + "</td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Câu hỏi trí nhớ:</td>" +
                    "              <td class=\"Col2\"><input type=checkbox id=chkIsMemory></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td class=\"Col1\">Thời gian ẩn:</td>" +
                    "              <td class=\"Col2\"><input id=txtSecondHidden value=\"30\"></td>" +
                    "          </tr>" +
                    "          <tr>" +
                    "              <td></td>" +
                    "              <td><input type=type=button class=button  onclick=javascript:ClientSaveAddYesNo(); value=\"Ghi lại\"><input type=type=button class=button   onclick=\"javascript:window.history.back(-1);\" value=\"Quay lại\"></td>" +
                    "          </tr>" +
                    "      </table>" +
                    "</div>\r\n";


                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideAddYesNo(string SiteId, string MonthiId, string Title, int SelectedIndexAns, string MaxPoint, string SecondTime, bool IsMemory, string SecondHidden, string SelectLevel)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                string Type = "YesNo";
                
                if (Title.Trim().Length == 0) throw new Exception("Xin vui lòng nhập câu hỏi!");
                if (SelectedIndexAns <= 0) throw new Exception("Chưa xác định câu trả lời!");
                if (FunctionUtilities.checkInteger(MaxPoint) == false)
                {
                    throw new Exception("Dữ liệu điểm nhập không hợp lệ! Phải nhập kiểu số!");
                }
                return new QuestionBll().AddYesNoQuestion(null, SiteId, Type, MonthiId, Title, SelectedIndexAns, int.Parse(MaxPoint),int.Parse(SecondTime),IsMemory,int.Parse(SecondHidden),int .Parse(SelectLevel));
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetShowUpdateForm(string SiteId, string QuestionId)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut(); 
                QuestionCls QuestionCls = new QuestionBll().CreateModel(SiteId, QuestionId);
                string UpdateUrl = "";
                if (QuestionCls.Type.ToLower().Equals("yesno"))
                {
                    UpdateUrl = "UpdateYesNo.aspx?QuestionId="+QuestionId;
                }
                if (QuestionCls.Type.ToLower().Equals("bestanswer"))
                {
                    UpdateUrl = "UpdateBestAnswer.aspx?QuestionId="+QuestionId;
                }
                //if (QuestionCls.Type.ToLower().Equals("MultiChoice".ToLower()))
                //{
                //    UpdateUrl = Screen.BuildUrl(
                //        SiteId,
                //        new ParamCls[]{
                //        new ParamCls("WebPartId",new UpdateMultiChoice().WebPartId),
                //        new ParamCls("QuestionId",QuestionId)
                //    });
                //}
                //if (QuestionCls.Type.ToLower().Equals("FreeText".ToLower()))
                //{
                //    UpdateUrl = Screen.BuildUrl(
                //        SiteId,
                //        new ParamCls[]{
                //        new ParamCls("WebPartId",new UpdateFreeText().WebPartId),
                //        new ParamCls("QuestionId",QuestionId)
                //    });
                //}

                //if (QuestionCls.Type.ToLower().Equals("LargeFreeText".ToLower()))
                //{
                //    UpdateUrl = Screen.BuildUrl(
                //        SiteId,
                //        new ParamCls[]{
                //        new ParamCls("WebPartId",new UpdateFreeText().WebPartId),
                //        new ParamCls("QuestionId",QuestionId)
                //    });
                //}
                if (UpdateUrl.Trim().Length == 0)
                {
                    throw new Exception("Không cung cấp tính năng sửa chữa cho loại: " + QuestionCls.Type);
                }
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}